package com.wind.taobao.api.trade.item.convert
{
	import com.wind.taobao.api.trade.item.model.ItemImg;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;

	/**
	 * ItemImg转换器
	 * @author fxy
	 */
	public class ItemImgConvert
	{
		/**
		 * 转换ItemImg集合
		 */
		public static function convertItemImgs(value:Object, type:String="json"):ArrayCollection
		{
			var itemImgs:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							itemImgs.addItem(convertItemImg((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return itemImgs;
		}

		/**
		 * 转换ItemImg
		 */
		public static function convertItemImg(value:Object, type:String="json"):ItemImg
		{
			var itemImg:ItemImg=new ItemImg();
			if (value == null)
				return itemImg;
			if (type == TaobaoConstants.XML)
			{
				itemImg=convertXMLToItemImg(XML(value), itemImg);
			}
			else
			{
				itemImg=convertJSONToItemImg(value, itemImg);
			}
			return itemImg;
		}

		/**
		 * JSON转换为ItemImg
		 */
		private static function convertJSONToItemImg(json:Object, itemImg:ItemImg):ItemImg
		{
			if (json.hasOwnProperty(TaobaoConstants.ITEMIMG_ID))
			{
				itemImg.itemimgId=json[TaobaoConstants.ITEMIMG_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.URL))
			{
				itemImg.url=json[TaobaoConstants.URL];
			}
			if (json.hasOwnProperty(TaobaoConstants.POSITION))
			{
				itemImg.position=json[TaobaoConstants.POSITION];
			}
			return itemImg;
		}

		/**
		 * XML转换为ItemImg
		 */
		private static function convertXMLToItemImg(value:XML, itemImg:ItemImg):ItemImg
		{
			return itemImg;
		}

	}
}